<?php

error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 1);

function url_dirname($path, $level=1)
{
	//$path = preg_replace('#\\\\+|/+#', '/', $path);
	while ($level-- > 0) $path = dirname($path);
	if (strpos($path, '\\') !== false) $path = str_replace('\\', '/', $path);
	return $path === '/' ? $path : $path . '/';
}
$_base_dir = url_dirname(__FILE__);

$site_config = $_base_dir.'etc/config.php';
if (is_file($site_config)) {

	require_once $_base_dir.'lib/kernel.php';

	class object {}

	global $FU;

	$FU      = new object ();
	$FU->dir = new object ();
	$FU->env = new object ();

	$FU->dir->install  = $_install_dir;
	$FU->dir->config   = $_config_dir;
	$FU->dir->lib      = $_lib_dir;
	$FU->dir->cache    = $_cache_dir;
	$FU->dir->state    = $_state_dir;

	$FU->env->name     = $_environment;

	$FU->charset       = 'utf-8';
	$FU->debug         = FALSE;
	$FU->timezone      = 0;       // GMT+5

	$FU->name          = 'funca';
	$FU->admin_email   = 'nop@jetstyle.ru';
	$FU->cookie_prefix = $FU->name.'_';

	$FU->widgets->dir  = $FU->dir->lib.'Action/Community/widgets';

	// --- handle --- {{{
	//
	debug($_SERVER['REQUEST_METHOD'].': '.$_SERVER['REQUEST_URI']);

	// lucky : не ну а чо?
	if (ini_get('expose_php')) header('X-Powered-By: KARNI/0.0');

	require_once $FU->dir->lib.'RL/SiteRequestHandler.php';

	checkpoint(__METHOD__,'RH create');
	$rh = new RL_SiteRequestHandler($site_config);

	checkpoint(__METHOD__,'RH initialize');
	$rh->initialize();

	// стартуем
	checkpoint(__METHOD__,'RH handle request');
	echo $rh->handleRequest();

	if (isset($rh->GET['debug'])) {

		if (function_exists('memory_get_usage')) {
			echo $rh->ctx->debug->trace("memusage: ".memory_get_usage());
		}
		echo $rh->ctx->debug->flush();

	}

	checkpoint(__METHOD__,'finalize');

	$rh->finalize();
} else {
	header('Location: install/index.php');
}

//
// --- handle --- }}}

